package com.csun.nusing.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.BedTimeJsonBean;
import com.csun.nusing.bean.jsonbean.BedTimeJsonBean.BedTimeDataBean;
import com.csun.nusing.bean.jsonbean.DeviceWatchJsonBean;
import com.csun.nusing.bean.jsonbean.DeviceWatchJsonBean.DeviceWatchDataBean;
import com.csun.nusing.bean.jsonbean.SelectPhoneAlarmDetailJsonBean;
import com.csun.nusing.bean.jsonbean.SelectPhoneAlarmDetailJsonBean.SelectPhoneAlarmDetailDataBean;
import com.csun.nusing.presenter.OldrestDetailsPresenter;
import com.csun.nusing.presenter.inter.IBase;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.myview.RoundImageView;
import com.csun.nusing.util.SPUtils;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;

/**
 * 老人详情界面 ClassName: OldrestDetailsActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-23
 */
public class OldrestDetailsActivity extends
		BaseActivity<OldrestDetailsPresenter> implements IBase {

	private View suifangview;// 随访仪
	private View alarmview;// 报警器
	private View watchview;// 手表
	private View sleepview;// 床垫
	private String olderId;
	private String olderName;
	private RoundImageView headiv;
	private TextView nametv;
	private TextView sexagetv;
	private TextView birtv;
	private TextView chuanghaotv;
	private TextView egnametv;
	private TextView egcodetv;
	private String olderPhotopath;
	private String olderBir;
	private String olderSex;
	private String olderEcgname;
	private String olderEcgPhone;
	private String birday;
	private String birmon;
	private LinearLayout suifangll;
	private LinearLayout alarmll;
	private LinearLayout watchll;
	private LinearLayout sleepll;
	private TextView sleepCodetv;
	private TextView xintiaotv;
	private TextView zaichaungstatetv;
	private TextView huxitv;
	private TextView xintiaobaojintv;
	private TextView fanshenbaojintv;
	private String authorization;
	private OkHttpClient client;
	private TextView watchcodetv;
	private TextView watchactiontv;
	private TextView watchxintiaotv;
	private TextView watchxueyatv;
	private TextView watchplacetv;
	private TextView alarmcodetv;
	private TextView alarmstatetv;
	private TextView alarmxinhaotv;
	private TextView alarmdiancitv;
	private TextView alarmdianyatv;
	protected DeviceWatchJsonBean nursingdevicewatchbean;
	private TextView alarmplace;
	protected SelectPhoneAlarmDetailJsonBean nursingdevicealarmbean;
	private String olderage;
	private String roombed;
	protected String responseString;
	protected BedTimeJsonBean bedtimebean;
	private ScheduledExecutorService scheduledExecutorService;
	private String roomId;
	public static final MediaType JSON = MediaType
			.parse("application/json; charset=utf-8");

	@Override
	public int getContentViewId() {
		return R.layout.activity_nursing_oldrestdetails;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
		controlView();
	}

	/**
	 * 初始控件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-23
	 */
	private void findView() {
		suifangview = findViewById(R.id.oldrest_details_suifang_view);
		alarmview = findViewById(R.id.oldrest_details_alarm_view);
		watchview = findViewById(R.id.oldrest_details_watch_view);
		sleepview = findViewById(R.id.oldrest_details_sleep_view);
		suifangll = findViewById(R.id.oldrest_details_suifang_ll);
		alarmll = findViewById(R.id.oldrest_details_alarm_ll);
		watchll = findViewById(R.id.oldrest_details_watch_ll);
		sleepll = findViewById(R.id.oldrest_details_sleep_ll);

		headiv = findViewById(R.id.olderdetail_oldhead_riv);
		nametv = findViewById(R.id.olderdetail_oldername_tv);
		sexagetv = findViewById(R.id.olderdetail_detail_sexage_tv);
		birtv = findViewById(R.id.olderdetail_detail_bir_tv);
		chuanghaotv = findViewById(R.id.olderdetail_detail_chuanghao_tv);
		egnametv = findViewById(R.id.olderdetail_detail_egname_tv);
		egcodetv = findViewById(R.id.olderdetail_detail_egcode_tv);

		// 床垫
		sleepCodetv = findViewById(R.id.olderdetail_chuangdian_code_tv);
		zaichaungstatetv = findViewById(R.id.olderdetail_chuangdian_zaichaung_state_tv);
		xintiaotv = findViewById(R.id.olderdetail_chuangdian_xintiao_state_tv);
		huxitv = findViewById(R.id.olderdetail_chuangdian_huxi_state_tv);
		xintiaobaojintv = findViewById(R.id.olderdetail_chuangdian_huxibaojin_state_tv);
		fanshenbaojintv = findViewById(R.id.olderdetail_chuangdian_fanshenbaojin_state_tv);
		// end

		/**
		 * 手表
		 */
		watchcodetv = findViewById(R.id.olderdetail_watch_code_tv);
		watchactiontv = findViewById(R.id.olderdetail_watch_action_state_tv);
		watchxintiaotv = findViewById(R.id.olderdetail_watch_xintiao_state_tv);
		watchxueyatv = findViewById(R.id.olderdetail_watch_xueya_state_tv);
		watchplacetv = findViewById(R.id.olderdetail_watch_place_state_tv);

		/**
		 * 报警器
		 */
		alarmcodetv = findViewById(R.id.olderdetail_alarm_code_tv);
		alarmstatetv = findViewById(R.id.olderdetail_alarm_state_tv);
		alarmplace = findViewById(R.id.olderdetail_alarm_place_tv);
		alarmxinhaotv = findViewById(R.id.olderdetail_alarm_xinhao_state_tv);
		alarmdiancitv = findViewById(R.id.olderdetail_alarm_dianci_state_tv);
		alarmdianyatv = findViewById(R.id.olderdetail_alarm_dianya_state_tv);

	}

	/**
	 * 点击事件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-24
	 */
	private void controlView() {
		suifangview.setOnClickListener(new click());
		alarmview.setOnClickListener(new click());
		watchview.setOnClickListener(new click());
		sleepview.setOnClickListener(new click());

	}

	@Override
	public void initData() {
		super.initData();
		olderId = getIntent().getStringExtra("olderId");
		olderName = getIntent().getStringExtra("olderName");
		olderPhotopath = getIntent().getStringExtra("olderPhotopath");
		roomId = getIntent().getStringExtra("roomId");
		Glide.with(this).load("" + olderPhotopath).into(headiv);
		nametv.setText("" + olderName);
		olderBir = getIntent().getStringExtra("olderBir");
		olderSex = getIntent().getStringExtra("olderSex");
		olderage = getIntent().getStringExtra("olderage");
		olderEcgname = getIntent().getStringExtra("olderEcgname");
		olderEcgPhone = getIntent().getStringExtra("olderEcgPhone");
		authorization = (String) SPUtils.get(this, "authorization", "");
		roombed = getIntent().getStringExtra("roombed");
		if (olderBir != null && olderBir.length() > 7) {
			// biryear = Integer.parseInt(olderBir.substring(0, 4));
			// Calendar cal = Calendar.getInstance();
			// int year = cal.get(Calendar.YEAR);
			// oldAge = year - biryear;
			if (olderSex.equals("0")) {
				sexagetv.setText("性别:男  年龄:" + olderage + "岁");
			} else {
				sexagetv.setText("性别:女  年龄:" + olderage + "岁");
			}
			// 1914-05-22
			String[] ss = olderBir.split("-");
			birmon = ss[1];
			birday = ss[2];
			birtv.setText("生日:" + birmon + "月" + birday + "日");
		}
		egnametv.setText("紧急联系人:" + olderEcgname);
		egcodetv.setText("联系人电话:" + olderEcgPhone);
		chuanghaotv.setText("房间床号:" + roombed);
		requsetsleepData();
		requsetwatchData();
		requsetalarmData();
		// 定时请求数据
		scheduledExecutorService = Executors.newScheduledThreadPool(1);
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				requsetsleepData();
				requsetwatchData();
				requsetalarmData();
			}
		}, 1, 1, TimeUnit.MINUTES);
	}

	/**
	 * 报警器
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void requsetalarmData() {
		if (roomId==null||roomId.equals("null")) {
			return;
		}
		Map<String, String> param = new HashMap<String, String>();
		param.put("roomId", "" + roomId);
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							nursingdevicealarmbean = gson.fromJson(result,
									SelectPhoneAlarmDetailJsonBean.class);
							if (nursingdevicealarmbean != null) {
								if (nursingdevicealarmbean.getData() != null) {
									controlAlarmList(nursingdevicealarmbean
											.getData());
								}
							}
						}
					}
				}).start(UrlBean.SELECTPHONEDATEBYROOMURL);

	}

	/**
	 * 报警器数据处理
	 * 
	 * @Description: TODO
	 * @param @param data
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	protected void controlAlarmList(List<SelectPhoneAlarmDetailDataBean> list) {
		if (list != null
				&& list.size() != 0) {
			alarmcodetv.setText(""+ list.get(0).getDeviceNo());
			// alarmstatetv.setText("");
			if (list.get(0).getStatus() == -1) {
				alarmstatetv.setText("掉线");
			}else if(list.get(0).getStatus() == 0) {
				alarmstatetv.setText("布防");
			}else if(list.get(0).getStatus() == 1) {
				alarmstatetv.setText("撤防");
			}
			alarmplace.setText(""+list.get(0).getRoomId());
			alarmdianyatv.setText(""+list.get(0).getSvtg());
			alarmdiancitv.setText(""+list.get(0).getOvtg());
			alarmxinhaotv.setText(""+list.get(0).getSignal());
		}
	}

	private void requsetwatchData() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("id", "" + olderId);
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							nursingdevicewatchbean = gson.fromJson(result,
									DeviceWatchJsonBean.class);
							if (nursingdevicewatchbean != null) {
								if (nursingdevicewatchbean.getData() != null) {
									controlWatchList(nursingdevicewatchbean
											.getData());
								}
							}
						}
					}
				}).start(UrlBean.WATCHDEVICEURL);

	}

	/**
	 * 显示手表
	 * 
	 * @Description: TODO
	 * @param @param list
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	protected void controlWatchList(DeviceWatchDataBean deviceWatchDataBean) {
		if (deviceWatchDataBean.getRealtime() != null) {
			watchcodetv.setText("智能手表");
			watchactiontv.setText("运动");
			watchxintiaotv.setText(""
					+ deviceWatchDataBean.getRealtime().getHeartrate());
			watchxueyatv.setText(deviceWatchDataBean.getRealtime().getDbp()
					+ "/" + deviceWatchDataBean.getRealtime().getSbp());
			watchplacetv.setText(""
					+ deviceWatchDataBean.getRealtime().getLocation());

		}
	}

	/**
	 * 加载床垫数据
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-3
	 */
	private void requsetsleepData() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("id", "" + olderId);
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "-------" + result);
							Gson gson = new Gson();
							bedtimebean = gson.fromJson(result,
									BedTimeJsonBean.class);
							if (bedtimebean != null) {
								if (bedtimebean.getData() != null) {
									controlSleepData(bedtimebean.getData());
								}
							}
						}
					}
				}).start(UrlBean.SLEEPREALTIMEYURL);

	}

	protected void controlSleepData(BedTimeDataBean data) {
		sleepCodetv.setText(data.getSleepId());
		if (data.isInBedStatus()) {
			zaichaungstatetv.setText("在床");
		} else {
			zaichaungstatetv.setText("离床");
		}
		xintiaotv.setText(data.getHeartbeat() + " ");
		huxitv.setText(data.getBreath() + " ");
		if (data.getAlarm() == 1) {
			xintiaobaojintv.setText("报警");
		} else {
			xintiaobaojintv.setText("无");
		}
		if (data.getAction() == 0) {
			fanshenbaojintv.setText("报警");
		} else {
			fanshenbaojintv.setText("无");
		}
	}

	/**
	 * 
	 * ClassName: click
	 * 
	 * @Description: TODO00
	 * @author fzq
	 * @date 2017-10-24
	 */
	private class click implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.oldrest_details_suifang_view:
				setLeanGone();
				suifangview
						.setBackgroundResource(R.drawable.laorenxiangqing_shebei_xuanzhong_hg);
				intent = new Intent(OldrestDetailsActivity.this,
						SuiFangDeviceActivity.class);
				intent.putExtra("olderId", olderId);
				startActivity(intent);
				suifangll.setVisibility(View.VISIBLE);
				break;
			case R.id.oldrest_details_alarm_view:
				setLeanGone();
				alarmview
						.setBackgroundResource(R.drawable.laorenxiangqing_shebei_xuanzhong_hg);
				alarmll.setVisibility(View.VISIBLE);
				break;
			case R.id.oldrest_details_watch_view:
				setLeanGone();
				watchview
						.setBackgroundResource(R.drawable.laorenxiangqing_shebei_xuanzhong_hg);
				watchll.setVisibility(View.VISIBLE);
				break;
			case R.id.oldrest_details_sleep_view:
				setLeanGone();
				sleepview
						.setBackgroundResource(R.drawable.laorenxiangqing_shebei_xuanzhong_hg);
				sleepll.setVisibility(View.VISIBLE);
				break;
			}
		}
	}

	@Override
	protected OldrestDetailsPresenter getPresenter() {
		return new OldrestDetailsPresenter(this);
	}

	public void setLeanGone() {
		suifangview.setBackgroundResource(R.drawable.kongbai);
		alarmview.setBackgroundResource(R.drawable.kongbai);
		watchview.setBackgroundResource(R.drawable.kongbai);
		sleepview.setBackgroundResource(R.drawable.kongbai);
		sleepll.setVisibility(View.GONE);
		watchll.setVisibility(View.GONE);
		alarmll.setVisibility(View.GONE);
		suifangll.setVisibility(View.GONE);
	}

}
